#include "log4xecho.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace log4x;

class MyListener : public ilog4x_listener
{
protected:
    virtual void       echo(const string& line, int level, const vector<log4x_hl_t>& hlsearch)
    {
        printf("%d %s", level, line.c_str());
        if (hlsearch.size() > 0)
        {
            for (size_t i = 0; i < hlsearch.size(); ++i)
            {
                cout << i << " " << line.substr(hlsearch[i].pos, hlsearch[i].len) << endl;
            }
        }
    }
};

int main(int argc, char *argv[])
{
    ilog4x_echo *echo = ilog4x_echo::create();

    vector<string> pids, keys;
    string path, pname;
    /* echo->getProcessID("chrome.exe", pids); */
#ifdef _WIN32
    //if (echo->getInfo("C:\\Users\\zhongcaiwang\\Desktop\\log4x\\log4x_test_staticd.exe", path, pname, pids, keys) < 0)
    if (echo->getInfo("C:\\cyg-contron\\p90\\bin\\cyg-middle.exe", path, pname, pids, keys) < 0)
    //if (echo->getInfo("C:\\cyg-contron\\p90\\bin\\cyg-dbs.exe", path, pname, pids, keys) < 0)
#else
    /* if (echo->getInfo("/home/caiwang213/workspace-win7/git/3rd-party/log4x/build-linux/install/bin/log4x_test_staticd", path, pname, pids) < 0) */
    if (echo->getInfo("/home/caiwang213/log4x_echo/log4x_test_staticd", path, pname, pids, keys) < 0)
#endif
    {
        exit(1);
    }

    for (size_t i = 0; i < pids.size(); ++i)
    {
        printf("%s \n", pids[i].c_str());
    }

    /* string log = echo->getFileName(path, pname, "00184"); */
    /* string log = echo->getFileName(path, pname, "117130", "test1"); */
    string log = echo->getFileName(path, pname, "003544");
    //string log = echo->getFileName(path, pname, "017376");
    echo->start(log);

    MyListener listener;
    echo->setEcho(&listener);

    int c = 0;
    while ('q' != (c = getchar()))
    {
        switch (c)
        {
        case 'p':
            echo->pause(true);
            break;

        case 'P':
            echo->pause(false);
            break;

        case 'r':
            break;

        case '0':
            echo->filter(0);
            break;

        case '1':
            echo->filter(1);
            break;

        case '2':
            echo->filter(2);
            break;

        case '3':
            echo->filter(3);
            break;

        case '4':
            echo->filter(4);
            break;

        case '5':
            echo->filter(5);
            break;

        case 'k':
            echo->filter(-1, "abc");
            break;

        default:
            break;
        }
    }

    getchar();

    return 0;
}
